package com.activity.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.provider.ContactsContract;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activity.App;
import com.activity.bean.OpusBean;
import com.activity.bean.RemarkBean;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.widget.BannerIndicator;
import com.example.apple.ddhr_new.adapter.BaseAdapter;
import com.example.apple.ddhr_new.utils.ImageViewUtil;
import com.example.apple.ddhr_new.utils.StringUtil;
import com.lcodecore.ILabel;
import com.lcodecore.LabelLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

import static com.youth.banner.BannerConfig.NOT_INDICATOR;

public class OpusDetailAdapter extends BaseAdapter<RemarkBean> {



    public  interface ClickListener{
        public  void  commentClick(View view);
        public  void  praiseClick();
        public  void  replyClick(int groupPosition,int childPosition,View view);
        public  void  deleteClick();
        public  void  editClick();

    }
    public  ClickListener listener;
    public OpusBean opusBean;


    public OpusDetailAdapter(Context context) {
        super(context);
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_remark_cell;
    }


    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.header_opus_detail;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return getData().size();
    }



    @Override
    public void onBindHeaderViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {

        if (groupPosition == 0) {
            Banner banner = (Banner) holder.get(R.id.banner);
            BannerIndicator indicator = (BannerIndicator) holder.get(R.id.indicator);
            WindowManager m = ((Activity) mContext).getWindowManager();
            Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
            banner.getLayoutParams().height = (int) (d.getWidth()*3.0/5.0);
            banner.setIndicatorGravity(BannerConfig.CENTER);
            banner.isAutoPlay(false);
            banner.setBannerStyle(NOT_INDICATOR);

            //设置图片加载器
            banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    ImageViewUtil.loadImage((String) path, imageView, mContext);
                   ArrayList<String> imageSizes =opusBean.imageSizes();
                   if(imageSizes.size()==opusBean.images().size()){
                       String p =imageSizes.get(opusBean.images().indexOf(path));
                       ArrayList<String> pa =new ArrayList<String>(Arrays.asList(StringUtils.split(p,",")));
                       if(pa.size()==2){
                           banner.getLayoutParams().height =(int) (d.getWidth()*Float.valueOf(pa.get(1))/ Float.valueOf(pa.get(0)));
                       }

                   }


//                    imageView.setImageResource(R.drawable.meinv);
                }
            });

            banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    indicator.setCurrentPosition(position);

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            ArrayList images = new ArrayList();

            //设置图片集合
            banner.setImages(opusBean.images());
            indicator.setCellCount(opusBean.images().size());
            //banner设置方法全部调用完毕时最后调用
            banner.start();

            LabelLayout labelLayout = (LabelLayout) holder.get(R.id.label_me);
            ArrayList<ILabel> arrayList = new ArrayList();

            for (int i = 0; i < opusBean.tags().size(); i++) {
                int finalI = i;
                arrayList.add(new ILabel() {
                    @Override
                    public String getId() {
                        return "1";
                    }

                    @Override
                    public String getName() {
                        return opusBean.tags().get(finalI);
                    }
                });
            }

            labelLayout.setLabels(arrayList);

            TextView praise = holder.itemView.findViewById(R.id.tv_praise_num);
            ImageView iv_praise = holder.itemView.findViewById(R.id.iv_praise);
            praise.setText(opusBean.getWorkspraisenum());

            if(opusBean.getWorkifpraise()!=null&&opusBean.getWorkifpraise().equals("1")) {
                iv_praise.setImageResource(R.drawable.dianzan1);
            }else {
                iv_praise.setImageResource(R.drawable.dianzan);
            }
            TextView remark = holder.itemView.findViewById(R.id.tv_remark_num);

            remark.setText("所有评论(" + opusBean.getWorksreplynum() + ")");
            TextView content = holder.itemView.findViewById(R.id.tv_content);
            TextView tv_content_text = holder.itemView.findViewById(R.id.tv_content_text);
            TextView date = holder.itemView.findViewById(R.id.tv_date);

            TextView tv_delete = holder.itemView.findViewById(R.id.tv_delete);
            TextView tv_edit = holder.itemView.findViewById(R.id.tv_edit);
            tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.deleteClick();
                    }
                }
            });
            tv_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.editClick();
                    }
                }
            });

            if(opusBean.getWorksuserid().equals(App.getInstance().getAccount().getUserid())){
                tv_delete.setVisibility(View.VISIBLE);
                tv_edit.setVisibility(View.VISIBLE);
            }else {
                tv_delete.setVisibility(View.GONE);
                tv_edit.setVisibility(View.GONE);
            }
            date.setText(opusBean.getWorksaddtime());
            content.setText(opusBean.getWorkstitle());
            tv_content_text.setText(opusBean.getWorkscontent());
            holder.itemView.findViewById(R.id.tv_comment).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(listener!=null){
                        listener.commentClick(v);
                    }
                }
            });
            praise.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.praiseClick();
                    }
                }
            });
            iv_praise.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.praiseClick();
                    }
                }
            });

        }

    }

    @Override
    public void onBindChildViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition, int childPosition) {
        super.onBindChildViewHolder(holder, groupPosition, childPosition);

        ((OpusDetailViewHolder)holder).tvReply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.replyClick(groupPosition,childPosition,((OpusDetailViewHolder)holder).tvHuifu.getVisibility()==View.GONE?((OpusDetailViewHolder)holder).tvReply:((OpusDetailViewHolder)holder).tvHuifu);
                }
            }
        });
    }

    @Override
    public BaseViewHolder getViewHolder(int viewType, View view) {
        if (viewType == TYPE_CHILD)
            return new OpusDetailViewHolder(view);

        return null;
    }


    public class OpusDetailViewHolder extends BaseViewHolder<RemarkBean> {
        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_huifu)
        TextView tvHuifu;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_reply)
        TextView tvReply;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.l_bg)
        LinearLayout lbg;
        public OpusDetailViewHolder(@NonNull View itemView) {
            super(itemView);
        }


        @Override
        public void setData(RemarkBean data) {


            lbg.removeAllViews();
            if(data.getReplylist()!=null&&data.getReplylist().size()>0){


                for (int i =0;i<data.getReplylist().size();i++){
                    RemarkBean.ReplylistBean bean = data.getReplylist().get(i);
                    View view = View.inflate(mContext, R.layout.replay_item, null);
                    ImageView imageView =view.findViewById(R.id.iv_icon);
                    TextView textViewName =view.findViewById(R.id.tv_name);
                    TextView textViewContent =view.findViewById(R.id.tv_content);
                    TextView textViewTime =view.findViewById(R.id.tv_time);
                    ImageViewUtil.loadCircleImage(bean.getUsericon(),imageView,mContext);
                    textViewName.setText(bean.getUsernickname());
                    textViewContent.setText(StringUtil.utfString(bean.getReplycontent()));
                    textViewTime.setText(bean.getReplyaddtime());
                    lbg.addView(view);
                }
//                tvHuifu.setVisibility(View.VISIBLE);
//                tvHuifu.setText(data.getReplayListString());
            }else {
//                tvHuifu.setVisibility(View.GONE);
            }


            ImageViewUtil.loadCircleImage(data.getUsericon(),ivIcon,mContext);
            tvName.setText( data.getUsernickname());
            tvContent.setText( data.getReplycontent());
            tvTime.setText(data.getReplyaddtime());
            tvReply.setText("回复");


        }


    }


}
